#include "dominion.h"
#include "dominion_helpers.h"
#include "sput/sput.h"
#include <stdlib.h>
#include <time.h>

struct gameState *testState;

static void test_card(void)
{
    int *bonus = NULL;

    int card = sea_hag;

    int choice1 = -1;
    int choice2 = -1;
    int choice3 = -1;

    int init_deck_count;
    int init_hand_count;
    int init_num_buys;
    int post_discard_count;

    int handPos;
    int player;
    int res;
    int numPlayers;
    int i;

    int init_deck_counts[MAX_PLAYERS];
    int init_deck_tops[MAX_PLAYERS];
    int init_discard_counts[MAX_PLAYERS];


    int kc[10] = {adventurer, council_room, feast, gardens, mine, remodel,
                  smithy, village, baron, great_hall
                 };

    // iterate through all player counts
    for(numPlayers = 2; numPlayers <= MAX_PLAYERS; numPlayers++) {
        initializeGame(numPlayers, kc, rand(), testState);

        //iterate over players
        for(player = 0; player < testState->numPlayers; player++) {
            // set turn to current player
            testState->whoseTurn = player;

            // place card in current hand position
            handPos = 0;
            testState->hand[player][handPos] = card;

            init_hand_count = testState->handCount[player];
            init_deck_count = testState->deckCount[player];
            // set buys to one to initialize turn
            init_num_buys = testState->numBuys = 1;

            // gather statistics
            for(i = 0; i < testState->numPlayers; i++) {
                init_deck_counts[i] = testState->deckCount[i];
                init_deck_tops[i] = testState->deck[i][testState->deckCount[i] - 1];
                init_discard_counts[i] = testState->discardCount[i];
            }

            // play the card
            res = cardEffect(card, choice1, choice2, choice3, testState, handPos, bonus);

            /* HERE'S WHERE WE TEST THE CARD'S EFFECT */

            for (i = 0; i < testState->numPlayers; i++) {
                if (i != player) {
                    post_discard_count = testState->discardCount[i];
                    sput_fail_unless(testState->deck[i][testState->deckCount[i] - 1] == curse, "curse card on top of deck");
                    sput_fail_unless(testState->deckCount[i] == init_deck_counts[i], "same number of cards in deck as before");
                    sput_fail_unless(post_discard_count == init_discard_counts[i] + 1, "incremented discard count by one");
                    sput_fail_unless(testState->discard[i][post_discard_count - 1] == init_deck_tops[i], "discarded top of deck");
                }
            }

            /* END TESTING CARD EFFECT */

        }   /* player */
    }   /* numPlayers */
}

int main(int argc, char *argv[])
{
    // Seed random number generator
    srand(time(NULL));

    // Initialize game state
    testState = newGame();

    sput_start_testing();
    sput_enter_suite(__FILE__);

    sput_run_test(test_card);
    sput_finish_testing();

    free(testState);

    return sput_get_return_value();
}
